package cn.restcloud.example.controller;

import java.io.File;
import java.util.List;

import org.apache.commons.fileupload.FileItem;

import cn.restcloud.framework.attachment.service.AttachmentUtil;
import cn.restcloud.framework.core.annotation.bean.BeanConfig;
import cn.restcloud.framework.core.annotation.bean.BeanType;
import cn.restcloud.framework.core.annotation.rest.GetParam;
import cn.restcloud.framework.core.annotation.rest.MethodType;
import cn.restcloud.framework.core.annotation.rest.Param;
import cn.restcloud.framework.core.annotation.rest.Params;
import cn.restcloud.framework.core.annotation.rest.RestConfig;
import cn.restcloud.framework.core.annotation.rest.RestMediaType;
import cn.restcloud.framework.core.context.AppContext;
import cn.restcloud.framework.core.util.DateTimeUtil;
import cn.restcloud.framework.core.util.PrintUtil;
import cn.restcloud.framework.validate.ValidType;

/**
 * 附件上传功能实现
 * 关键是要把 consumes="multipart/form-data" 注解即可
 * @author Administrator
 *
 */
@BeanConfig(appId = "example",beanId = "AttachmentDemo",beanName = "文件上传示例",beanType = BeanType.CONTROLLER,autowired = true,singleton = true,hotMode = true)
@RestConfig(url = "/rest/example/uploadfile")
public class AttachmentDemo {
	
	
	/**
	 * 附件上传服务需要注解consumes="multipart/form-data", requestBody=false 时才可自动接收文件对像
	 * @param 注解中使用mode=ParamMode.FILE可以对文件上传控件进行验证
	 * @param fileItemss
	 * @return
	 * @throws Exception
	 */
	@RestConfig(configName = "文件上传获取FileItem对像",method = MethodType.POST,url = "/fileitem",consumes="multipart/form-data")
    @Params({
        @Param(id = "age", name = "表单参数",type="int",sampleValue="35",required=true),
        @Param(id = "file", name = "文件上传控件",type="file",validateBeanId=ValidType.VALIDFILEUPLOAD,validateBeanArgs=".xml,.doc,.docx",required=true),
    })
	public String uploadfileitem(List<FileItem> fileItems,@GetParam("age")String age) throws Exception{

		//也可以直接用以下代码获取
//		List<FileItem> fileItems=RequestUtil.getUploadFileItems();
		
		String fileName="";
		for(FileItem fileItem:fileItems){
           	 fileName+=" "+fileItem.getName(); //上传的文件名
           	 String baseDir=AttachmentUtil.getAttachmentBaseDir(AttachmentUtil.RESOURCEPATH); //AttachmentsPath|ResourcePath可选这两个id
             String monthNum=DateTimeUtil.getNow("yyyyMM");
             String fileDir =baseDir+"/"+AppContext.getAppId()+"/"+monthNum+"/"; 
             File dir = new File(fileDir);
             if (!dir.exists()) { dir.mkdirs();} //如果目录不存在则创建一个
             PrintUtil.o("上传附件到:"+fileDir+fileItem.getName());
             File newFile=new File(fileDir+fileItem.getName()); //上传到资源目录的应用目录下
             fileItem.write(newFile); //写入到硬盘路径中去,如果不写入在硬盘中时需要执行fileItem.delete()删除临时文件
		}
		
		return "文件上传成功("+fileName+") age="+age;
	}

	@RestConfig(configName = "文件上传直接获取File对像",method = MethodType.POST,url = "/file",consumes=RestMediaType.MULTIPART_FORM_DATA_VALUE)
    @Params({
        @Param(id = "age", name = "表单参数",type="int",sampleValue="35",required=true),
    })
	public String uploadfile(List<File> fileItems,@GetParam("age")String age) throws Exception{
		String fileName="";
		for(File fileItem:fileItems){
           	 fileName+=" "+fileItem.getPath(); //上传的文件，已经保存到app的应用目录下了,如果不需要时需要把file删除掉
		}
		return "文件上传成功("+fileName+") age="+age;
	}
	
}
